package com.spring.oa.web.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.oa.api.web.dao.OaMapper;
import com.spera.genutil.Token;
import com.spera.genutil.TokenUtil;
import com.spring.oa.web.service.OaUserService;
import com.spring.security.core.util.ApplicationUtils;
import com.spring.security.web.dao.JedisCache;
import com.spring.security.web.model.User;

@Service
public class OaUserServiceImpl implements OaUserService{

    @Resource
    private OaMapper oaMapper;
	
	@Resource
	private JedisCache jedisCache;
	
	@Override
	public Map<String, Object> login(String token, String userName,
			String passWord) throws Exception {

		Map<String, Object> m = ApplicationUtils.getDataSet();
		if (ApplicationUtils.isNotNull(token)) {
			Map<String, Object> s = TokenUtil.checkToken(token,jedisCache);
			if(ApplicationUtils.isNotNull(s)){
				return s;
			}else{
//				String id=jedisCache.lindex(token,0);
				String id=jedisCache.hget(token,TokenUtil.USER_ID);
				//刷新token时间
				User u = new User();
				u.setId(id);
				Map<String,Object> p = new HashMap<String,Object>();
				p.put("user_id", id);
				List<Map<String,Object>> au= oaMapper.queryOaUser(p);
				m.put("token", token);
				m.put(ApplicationUtils.DATASET, au.get(0));
				m.put(ApplicationUtils.STATUS, "1");
				m.put(ApplicationUtils.MSG, "登录成功");
				return m;
			}
		}

		if ((!ApplicationUtils.isNotNull(userName))
				|| (!ApplicationUtils.isNotNull(passWord))) {
			m.put(ApplicationUtils.STATUS, "0");
			m.put(ApplicationUtils.MSG, "用户名或密码不能为空");
			return m;
		}

		List<Map<String,Object>> l = oaMapper.checkOaUser(userName);
		if (l==null || (l.size() == 0)) {
			m.put(ApplicationUtils.STATUS, "0");
			m.put(ApplicationUtils.MSG, "用户名不存在");
			return m;
		}
		
		Map<String,Object> userMap = l.get(0);
		if (!passWord.equalsIgnoreCase(String.valueOf(userMap.get("password_mw")))) {
			m.put(ApplicationUtils.STATUS, "0");
			m.put(ApplicationUtils.MSG, "密码错误");
			return m;
		}
		
		Token tsession = new Token();
		tsession.setUser_id(ApplicationUtils.Object2String(userMap.get("user_id"))).setUser_name(ApplicationUtils.Object2String(userMap.get("user_name")))
				.setDept_id(ApplicationUtils.Object2String(userMap.get("dept_id")));
		String user_uuid = TokenUtil.SetSperaToken(null,jedisCache, tsession,TokenUtil.OA_LOGIN);
//		String user_uuid = "oa_"+ApplicationUtils.randomUUID();
		m.put("token", user_uuid);
		m.put(ApplicationUtils.STATUS, "1");
		m.put(ApplicationUtils.MSG, "登录成功");
		
//		jedisCache.rpush(user_uuid, ApplicationUtils.Object2String(userMap.get("user_id")));//0
//		jedisCache.rpush(user_uuid, ApplicationUtils.Object2String(userMap.get("user_name")));//1
//		jedisCache.rpush(user_uuid, ApplicationUtils.Object2String(userMap.get("dept_id")));//2
//		jedisCache.expire(user_uuid, JedisCont.JEDIS_EXPRISE);
		
		Map<String,Object> p = new HashMap<String,Object>();
		p.put("user_id", ApplicationUtils.Object2String(userMap.get("user_id")));
		List<Map<String,Object>> au= oaMapper.queryOaUser(p);
		m.put("dataset", au.get(0));
		return m;
	}

	@Override
	public Map<String, Object> queryOaUser(String token, String username,
			String dept_id, String include) {
		Map<String, Object> s = TokenUtil.checkToken(token,jedisCache);
		if(ApplicationUtils.isNotNull(s)){
			return s;
		}
		
		Map<String,Object> p = new HashMap<String,Object>();
		p.put("dept_id", dept_id);
		List<Map<String,Object>> dataSet = oaMapper.queryOaUser(p);
		if("1".equalsIgnoreCase(include)){//递归查询下级部门成员
			if(dataSet!=null&&dataSet.size()>0){
				List<Map<String,Object>> dataSet1 = new ArrayList<Map<String,Object>>();
				for(Map<String,Object> d:dataSet){
					dataSet1.add(d);
				}
				getOaUserAllByDepId(dataSet,dataSet1);
			}
		}
		
		s = new HashMap<String, Object>();
		s.put(ApplicationUtils.STATUS, "1");
		s.put(ApplicationUtils.MSG, "查询成功");
		s.put(ApplicationUtils.DATASET, dataSet);
		return s;
	}
	
	
	private void getOaUserAllByDepId(List<Map<String,Object>> m,List<Map<String,Object>> pl){
		HashMap<String,Object> p=new HashMap<String,Object>();
		List<Map<String,Object>> npl = new ArrayList<Map<String,Object>>(); 
//		List<Map<String,Object>> nd = null;
		if(pl!=null&&pl.size()>0){
			for(Map<String,Object> d:pl){
				if(ApplicationUtils.isNotNull(d.get("dept_id"))){
					p.put("dept_pid", d.get("dept_id"));
					List<Map<String,Object>> dataSet= oaMapper.queryOaUser(p);
					//将数据插入结果集
					for(Map<String,Object> data:dataSet){
						m.add(data);
						npl.add(data);
					}
				}
			}
			if(npl!=null&&npl.size()>0){
				getOaUserAllByDepId(m,npl);
			}
		}
	}
	
}
